package com.cskaoyan.shopping.service.impl.yyr;

import com.cskaoyan.mall.commons.exception.yyr.ExceptionProcessorUtils;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.yyr.HomePageResponse;
import com.cskaoyan.shopping.service.yyr.IHomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Description：
 * @Author: 杨涌仁
 * @Date: 2022/08/25/0025 9:29
 */
@Slf4j
@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    /*
     * @Function:
     * 商品主页信息显示
     * @Return:主页panel信息以及其类目下的商品
     * @author yyr
     */
    @Override
    public HomePageResponse homepage() {
        HomePageResponse homePageResponse = new HomePageResponse();

        try {
            // 利用position=0查询出listPanel及对应content信息
            List<Panel> panels = panelMapper.selectPanelContentByPosition(0);
            // 查询到每个content下商品的信息链表  并set进对应的content中
            for (Panel panel : panels) {
                panel.setPanelContentItems(panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId()));
            }
            Set<PanelDto> panelDtos = contentConverter.panels2PanelDto(panels);
            // 设置成功状态码
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            // 设置成功信息
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            // 设置返回数据
            homePageResponse.setPanelContentItemDtos(panelDtos);
            // 捕获异常
        } catch (Exception e) {
            // 打印异常信息
            log.error("IHomeServiceImpl.homepage occur Exception :" + e);
            // 利用异常处理工具类处理异常
            ExceptionProcessorUtils.wrapperHandlerException(homePageResponse, e);
        }
        // 没有异常则返回数据
        return homePageResponse;
    }
}
